1 package jrre.unittests.datastructures;
2
3 /***
4 * Linked List implementation to test the JRRE with.
5 *
6 * @author Christopher Ellsworth (Chris@chrisellsworth.com)
7 */
8 public class LinkedList {
9
10 /***
11 * Inner class for the lists node.
12 */
13 class Node {
14
15 private int value;
16 private Node next;
17 private Node previous;
18
19 public Node(){}
20 public Node(int value, Node previous, Node next){
21 this.previous = previous;
22 this.value = value;
23 this.next = next;
24 }
25
26 public int getValue(){ return this.value; }
27 public void setValue(int value){ this.value = value; }
28
29 public Node getPrevious(){ return this.previous; }
30 public void setPrevious(Node previous){ this.previous = previous; }
31
32 public Node getNext(){ return this.next; }
33 public void setNext(Node next){ this.next = next; }
34 }
35
36
37 private int count;
38
39 private Node head;
40 private Node tail;
41 private Node cursor;
42
43 public LinkedList(){}
44 public LinkedList(int [] data){
45
46 for(int i=0;i < data.length;i++){
47 append(data[i]);
48 }
49 }
50
51 public void insert(int value){
52
53 if(head == null){
54 head = new Node(value, null, null);
55 tail = head;
56 }
57 else {
58 head = new Node(value, null, head);
59 }
60 }
61
62 public void append(int value){
63
64 if(head == null){
65 head = new Node(value, null, null);
66 tail = head;
67 }
68 else {
69 tail.setNext(new Node(value, tail, null));
70 tail = tail.getNext();
71 }
72 }
73
74 public int removeHead(){
75 Node headNode = head;
76 head = head.getNext();
77 return headNode.getValue();
78 }
79
80 public int removeTail(){
81 int toReturn = tail.getValue();
82 tail = tail.getPrevious();
83 tail.setNext(null);
84 return toReturn;
85 }
86
87 public boolean find(int value){
88 cursor = head;
89 while(cursor != null){
90
91 if(cursor.getValue() == value)
92 return true;
93
94 cursor = cursor.getNext();
95 }
96 return false;
97 }
98
99 public void printList(){
100
101 cursor = head;
102 while(cursor != null){
103 println(cursor.getValue());
104 cursor = cursor.getNext();
105 }
106 }
107
108 public static void println(int toPrint){
109 jrre.api.java.lang.System.out.println(toPrint);
110 }
111
112 public static void println(String toPrint){
113 jrre.api.java.lang.System.out.println(toPrint);
114 }
115
116 public static void main(String [] args){
117
118 }
119 }
This page was automatically generated by Maven